<template>
  <el-form :model="formData" label-width="80px" ref="form" v-loading="loading" class="pr-4">
    <div class="flex flex-row justify-between">
      <div :class="hasRoles ? 'w-1/2' : 'w-full'">
        <el-form-item
          label="昵称"
          prop="username"
          :rules="[
            {
              required: true,
              message: '昵称必须填写'
            }
          ]"
        >
          <el-input v-model="formData.username" placeholder="请填写昵称" />
        </el-form-item>
        <el-form-item
          label="名称"
          prop="name"
         
        >
          <el-input v-model="formData.name" placeholder="请填写名称" />
        </el-form-item>
        <!-- :rules="[
            {
              required: true,
              message: '名称必须填写'
            }
          ]" -->
        <el-form-item
          label="邮箱"
          prop="email"
         
        >
        <!-- :rules="[
            {
              required: true,
              message: '邮箱必须填写'
            },
            {
              type: 'email',
              message: '邮箱格式不正确'
            }
          ]" -->
          <el-input v-model="formData.email" placeholder="请填写邮箱" />
        </el-form-item>
        <el-form-item label="角色" prop="roles" v-if="hasRoles" :rules="[{ required: true, message: '请选择角色' }]">
          <el-select v-model="formData.roles" placeholder="请选择">
            <el-option
              v-for="item in roleStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <!-- <el-tree-select
            v-model="formData.roles"
            :default-expand-all="true"
            :render-after-expand="false"
            :data="roles"
            value-key="id"
            check-strictly
            class="w-full"
            :props="{ label: 'role_name', value: 'id' }"
            clearable
            show-checkbox
            @change="selecchange"
            check-on-click-node
          /> -->
        </el-form-item>
          <el-form-item
              label="手机号"
              prop="phone"
              :rules="[
            {
              required: true,
              message: '手机号必须填写'
            },
          ]"
          >
              <el-input v-model="formData.phone" placeholder="请填写手机号" />
          </el-form-item>
        <el-form-item label="密码" prop="password" :rules="passwordRules">
          <el-input v-model="formData.password" type="password" show-password placeholder="请输入密码" />
        </el-form-item>

        
      </div>

      <div class="w-1/2" v-if="hasRoles">
        <el-form-item label="部门" prop="department_id">
          <el-tree-select v-model="formData.department_id" :data="departments" check-strictly :props="{ label: 'department_name', value: 'id' }" />
        </el-form-item>
        <el-form-item label="岗位" prop="department_id">
          <el-select v-model="formData.jobs" multiple>
            <el-option v-for="item in jobs" :key="item.id" :label="item.job_name" :value="item.id" />
          </el-select>
        </el-form-item>
      </div>
    </div>
    <div class="flex justify-end">
      <el-button type="primary" @click="submitForm(form)">{{ $t('system.confirm') }}</el-button>
    </div>
  </el-form>
</template>

<script lang="ts" setup>
// @ts-nocheck
import { useCreate } from '@/composables/curd/useCreate'
import { useShow } from '@/composables/curd/useShow'

import { onMounted, ref , onBeforeMount } from 'vue'
import http from '@/support/http'

const props = defineProps({
  primary: [String, Number],
  api: String,
  isAdmins:String,
  roleStatus:Array,
  isAdmin:String,
  hasRoles: {
    type: Boolean,
    default: true
  }
})
const sss = ref(3)
const passwordRules = [
  {
    required: true,
    message: '密码必须填写'
  },
  {
    pattern: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/,
    message: '必须包含大小写字母和数字的组合，可以使用特殊字符，长度在6-20之间'
  }
]

const selecchange = (e) =>{
  console.log(e)
}

if (props.primary) {
  passwordRules.shift()
}

const { formData, form, loading, submitForm, close } = useCreate(props.api, props.primary)

if (props.primary) {
  useShow(props.api, props.primary, formData)
}

const emit = defineEmits(['close'])
close(() => emit('close'))

const departments = ref()
const jobs = ref()
const roles = ref()




const roleStatus = ref()
onMounted(() => {
  console.log()
  if(props.isAdmin == 'true' || props.isAdmins == 'managger'){
  roleStatus.value = [{ value: 1, label: '平台管理员' },{value:2,label:'渠道经理'},{value:3,label:'分销员'}]
  }else if(props.isAdmins == 'LevelOne'){
    roleStatus.value = [{value:3,label:'分销员'}]
  }else if(props.isAdmins == 'LevelTwo'){
    roleStatus.value = [{value:3,label:'分销员'}]
  }
  if (props.hasRoles) {
    http.get('permissions/roles').then(r => {
      roles.value = r.data.data
    })
    http.get('permissions/departments').then(r => {
      departments.value = r.data.data
    })

    http.get('permissions/jobs').then(r => {
      jobs.value = r.data.data
    })
    // http.get('user/me').then(res => {
    //   isAdmins.value = res.data.data.roles[0].identify
    // })
    // http.get('user/is_admin').then(res => {
    //   isAdmin.value = res.data.data
    // })

    
  }
})


</script>
